﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;
using System.Linq;

namespace Yosca
{
	/// <summary>
	/// Interaction logic for ExpenseControl.xaml
	/// </summary>
	public partial class ExpenseControl : UserControl
	{
		public ExpenseControl()
		{
			this.InitializeComponent();
		}

		private void onAddExpenseClick(object sender, System.Windows.RoutedEventArgs e)
		{
			Expense exp = new Expense();
			IObjectContainer expDB;
        	if(expenseDate.Text == "" || payMethod.Text == "" || expensePayTo.Text == "" || expenseAmount.Text == "" || expenseDescription.Text == "")
			{
                MessageBox.Show("ERROR: All boxes must be filled before expense item can be added");
			}
			else
			{
                switch (payMethod.SelectedIndex)
                {
                    case 0:
                        exp.flags[0] = true;
                        break;
                    case 1:
                        exp.flags[1] = true;
                        break;
                    case 2:
                        exp.flags[2] = true;
                        break;
                }

                expDB = Db4oFactory.OpenFile("expDB.ass");
                
                exp.purchaseDate = new System.DateTime(expenseDate.SelectedDate.Value.Year, expenseDate.SelectedDate.Value.Month, expenseDate.SelectedDate.Value.Day);
				exp.payMethod = payMethod.Text;
                exp.whoToPay = expensePayTo.Text.ToUpper();
				exp.amount = Convert.ToDouble(expenseAmount.Text);
				exp.description = expenseDescription.Text.ToUpper();
				expDB.Store(exp);
				expDB.Commit();
				IEnumerable<Expense> EList = from Expense E in expDB
											 select E;
                expenseGrid.ItemsSource = EList.ToList<Expense>();
                clearExpensesFields();
                expDB.Close();
			}
		}
		private void clearExpensesFields()
        {
            expenseDate.SelectedDate = System.DateTime.Now;
            payMethod.Text = "";
            expensePayTo.Text = "";
            expenseAmount.Text = "";
            expenseDescription.Text = "";
        }
        public void populateExpenseGrid()
        {
            //expenseGrid.AutoGenerateColumns = false;
            DataGridTextColumn dateCol = new DataGridTextColumn();
            dateCol.Header = "Purchase Date";
            dateCol.Binding = new Binding("purchaseDate");
            dateCol.Binding.StringFormat = "MMMM dd, yyyy";
            expenseGrid.Columns.Add(dateCol);

            DataGridTextColumn amountCol = new DataGridTextColumn();
            amountCol.Header = "Amount Paid";
            amountCol.Binding = new Binding("amount");
            amountCol.Binding.StringFormat = "$0.00";
            expenseGrid.Columns.Add(amountCol);

            DataGridTextColumn payeeCol = new DataGridTextColumn();
            payeeCol.Header = "Paid to";
            payeeCol.Binding = new Binding("whoToPay");
            expenseGrid.Columns.Add(payeeCol);

            DataGridTextColumn descCol = new DataGridTextColumn();
            descCol.Header = "Description";
            descCol.Binding = new Binding("description");
            expenseGrid.Columns.Add(descCol);

            DataGridTextColumn payMethodCol = new DataGridTextColumn();
            payMethodCol.Header = "Payment Method";
            payMethodCol.Binding = new Binding("payMethod");
            expenseGrid.Columns.Add(payMethodCol);

            expenseGrid.CanUserAddRows = false;
            IObjectContainer expDB = Db4oFactory.OpenFile("expDB.ass");
            IEnumerable<Expense> EList = from Expense E in expDB
                                         select E;
            if(EList.ToList() != null)
                expenseGrid.ItemsSource = EList.ToList<Expense>();
            expDB.Close();
        }
	}
}